import _ from 'lodash';
const state = () => ({
  subject: {
    subjectData: {},
    major_combine: {},
    isOneSelectSubject: false, //是否第一次进入智能选科
    tabsName: 'yuanxiao',
    collegeCheckedList: [], //院校优先已选择数据
    majorCheckedList: [], //专业优先已选择数据
    collegeChecked: [], //院校选中的数据
    majorChecked: [], //专业选中的数据
    selectedRows: new Set([]), //选中行的数据
    collegeSgid: '',
    majorSgid: '',
  },
});

// getters
const getters = {};

// actions
const actions = {
  setCollegeSgid({ commit }, sgid) {
    commit('set_collegeSgid', sgid);
  },
  setMajorSgid({ commit }, sgid) {
    commit('set_MajorSgid', sgid);
  },
  deleteSlectRows({ commit }, data) {
    commit('delete_selectedRows', data);
  },
  updateSlectRows({ commit }, data) {
    commit('update_selectedRows', data);
  },
  addSelectedRows({ commit }, data) {
    commit('add_selectedRows', data);
  },
  clearSelectRows({ commit }, data) {
    commit('clear_selectRows', data);
  },
  // 院校选中的数据
  changeCollegeChecked({ commit }, data) {
    commit('change_CollegeChe', data);
  },
  // 删除院校选中的数据
  removeCollegeChe({ commit }, index) {
    commit('remove_collegeChe', index);
  },
  //专业选中的数据
  changeMajorChecked({ commit }, data) {
    commit('change_MajorChe', data);
  },
  // 删除专业选中的数据
  removeMajorChe({ commit }, index) {
    commit('remove_MajorChe', index);
  },
  addCollegeData({ commit }, data) {
    commit('add_collegeData', data);
  },
  removeCollegeData({ commit }, data) {
    commit('remove_collegeData', data);
  },
  removeCollegeAll({ commit }, data) {
    commit('remove_collegeAll', data);
  },
  addMajorData({ commit }, data) {
    commit('add_MajorData', data);
  },
  removeMajorData({ commit }, data) {
    commit('remove_MajorData', data);
  },
  removeMajorAll({ commit }, data) {
    commit('remove_MajorAll', data);
  },
  changeTabsName({ commit }, tabName) {
    commit('set_TabsName', tabName);
  },
  chanIsOneSelectSubject({ commit }, isOne) {
    commit('set_IsOne', isOne);
  },
  changeMode({ commit }, type) {
    commit('set_mode', type);
  },
  changeCombine({ commit }, code) {
    commit('set_combine', code);
  },
};

// mutations
const mutations = {
  set_mode: (state, mode) => {
    state.subject.subjectData.mode = mode;
  },
  set_combine: (state, code) => {
    state.subject.major_combine.code = code;
  },
  set_IsOne: (state, isOne) => {
    state.subject.isOneSelectSubject = isOne;
  },
  set_TabsName: (state, tabName) => {
    state.subject.tabsName = tabName;
  },
  add_collegeData: (state, data) => {
    state.subject.collegeCheckedList.push(data);
  },
  remove_collegeData: (state, data) => {
    // const index = state.subject.collegeCheckedList.findIndex(
    //     (item) => item.id === data.id
    // );
    // state.subject.collegeCheckedList.splice(index, 1)
    state.subject.collegeCheckedList.splice(
      state.subject.collegeCheckedList.indexOf(data),
      1,
    );
  },
  remove_collegeAll: (state, data) => {
    state.subject.collegeCheckedList = data;
  },
  add_MajorData: (state, data) => {
    state.subject.majorCheckedList.push(data);
  },
  remove_MajorData: (state, data) => {
    state.subject.majorCheckedList.splice(data, 1);
  },
  remove_MajorAll: (state, data) => {
    state.subject.majorCheckedList = data;
  },
  change_CollegeChe: (state, data) => {
    state.subject.collegeChecked.push(data);
  },
  change_MajorChe: (state, data) => {
    state.subject.majorChecked.push(data);
  },
  remove_collegeChe: (state, index) => {
    state.subject.collegeChecked.splice(index, 1);
  },
  remove_MajorChe: (state, index) => {
    state.subject.majorChecked.splice(index, 1);
  },
  add_selectedRows: (state, data) => {
    state.subject.selectedRows.add(data.id);
  },
  update_selectedRows: (state, data) => {
    state.subject.selectedRows = new Set(data);
  },
  delete_selectedRows: (state, data) => {
    state.subject.selectedRows.delete(data.id);
  },
  clear_selectRows: (state, type) => {
    if (type == 0) {
      const collegeCheckedListIds = state.subject.collegeCheckedList.map(
        (v) => v.id,
      );
      state.subject.selectedRows = new Set(
        _.difference(
          Array.from(state.subject.selectedRows),
          collegeCheckedListIds,
        ),
      );
    } else {
      const majorCheckedList = state.subject.majorCheckedList.map((v) => v.id);
      state.subject.selectedRows = new Set(
        _.difference(Array.from(state.subject.selectedRows), majorCheckedList),
      );
    }
  },
  set_collegeSgid: (state, sgid) => {
    state.subject.collegeSgid = sgid;
  },
  set_MajorSgid: (state, sgid) => {
    state.subject.majorSgid = sgid;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
